<template>
  <div>
    <div style="margin-bottom: 10px">
      <el-input v-model="params.name" placeholder="请输入酒店名称" style="width: 200px"></el-input>
      <el-button type="warning" style="margin: 10px" @click="findBySearch()">查询</el-button>
      <el-button type="warning" style="margin: 10px" @click="reset()">清空</el-button>
    </div>

    <div class="about">
          <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column prop="hotelName" label="酒店名称" width="180"></el-table-column>
            <el-table-column prop="userName" label="预定人" width="180"></el-table-column>
            <el-table-column prop="time" label="预定时间"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" @click="edit(scope.row)" v-if="user.role === 'ROLE_ADMIN'">编辑</el-button>
                <el-button type="primary" @click="reserve(scope.row)" v-if="user.role !== 'ROLE_ADMIN'">退订</el-button>
                <el-popconfirm title="确定删除吗？" @confirm="del(scope.row.id)">
                  <el-button slot="reference" type="danger" style="margin-left: 5px" v-if="user.role === 'ROLE_ADMIN'">删除</el-button>
                </el-popconfirm>
              </template>
            </el-table-column>
          </el-table>
        </div>

        <!--  分页插件  -->
            <div style="margin-top: 10px">
              <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="params.pageNum"
                  :page-sizes="[5, 10, 15, 20]"
                  :page-size="params.pageSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
              </el-pagination>
            </div>



  </div>
</template>


<script>
import request from "@/utils/request";

export default {
  data() {
    return {
      url: "http://localhost:8080/api/files/",
      total: 0,
      params:{
        name: '',
        author: '',
        pageSize:5,
        pageNum:1
      },
      input:'',
      tableData: [],
      form: {},  //form表单对象
      dialogFormVisible: false,  //模态框显示/不显示

      typeObjs:[],  //存放查询出来的所有图书分类信息对象的数据
      user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {},
    }
  },
  // 页面加载时
  created() {
    this.findBySearch();
    //一打开图书信息页面时就要查询所有的分类信息
    this.findTypes();
  },
  // 页面按钮触发事件时使用的方法
  methods: {


    //2. 模糊查询 + 添加分页
    findBySearch(){
      request.get('/reserve/search',{params : this.params}).then(res => {
        if (res.code === '0'){
          this.tableData = res.data.list;
          this.total = res.data.total;
        }else {

        }
      })
    },

    //3. 清空查询框
    reset(){
      this.params = {
          phone: '',
          name: '',
          pageNum: 1,
          pageSize: 5
      }
      this.findBySearch()
    },

    //4. x条/页 切换
    handleSizeChange(pageSize){  //有回调函数，参数是每页显示条数
      this.params.pageSize = pageSize;
      this.findBySearch();
    },
    //5. 页数切换
    handleCurrentChange(pageNum){  //有回调函数，参数是页数
      this.params.pageNum = pageNum;
      this.findBySearch();
    },

    //6.新增图书
    add(){
      this.form = {};  //原本有个bug,每次新增前，先清空form表单就解决了
      this.dialogFormVisible = true;
    },
    submit(){
      request.post('/reserve',this.form).then(res => {
        if (res.code === '0'){
          //成功
          this.$message.success('操作成功')
          this.dialogFormVisible = false;
          this.findBySearch();
        }else {
          //失败
          this.$message.error(res.msg)
        }
      })
    },

    //7. 编辑图书
    edit(obj){
      this.form = obj;
      this.dialogFormVisible = true;
    },

    //8.删除
    del(id){
      request.delete('/reserve/'+id).then(res => {
        if (res.code === '0'){
          this.$message.success('操作成功')
          this.findBySearch()
        }else {
          this.$message.error(res.msg)
        }
      })
    },


    // 文件上传成功后的钩子:这里只是上传，还没点击确定，就是看能不能上传到成功
    successUpload(res){  // 回调参数
      // 把上传成功之后，后端返回值拿过来存储
      // 返回的是时间戳， 属于文件名的一部分，到时候把这个唯一值存储到数据库，就有了对应关系，就好办了。
      this.form.img = res.data;
    },
  }
}
</script>
